﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MLM.AdminCP.Class;
using MLM.Data;

namespace MLM.AdminCP.Pages
{
    public partial class ForgotPassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtLogin.Click += new EventHandler(txtLogin_Click);
        }

        void txtLogin_Click(object sender, EventArgs e)
        {
            if (txtUserId.Text.Trim() != "")
            {
                try
                {
                    MLMDataContext ct = new MLMDataContext();
                    long userId = long.Parse(txtUserId.Text);
                    User firstOrDefault = ct.Users.FirstOrDefault(it => it.UserId == userId);
                    if (firstOrDefault != null)
                    {
                        Email.Send_Email(ct.UserInfos.FirstOrDefault(it => it.UserID == userId).Email, "Your password",
                                     CommonSettingHelper.GetCommonSetting(ct,
                                         CommonSettingsContstants.EMAIL_SENDNEWPASSWORDTEMPLATE).Replace("{{USER_PASSWORD}}", firstOrDefault.Password));
                        txtError.Text = "Password has been sent to your email!";
                        txtError.ForeColor = Color.Green;
                    }
                }
                catch (Exception)
                {
                    txtError.Text = "Recovery fail! Please check your information!";
                }
            }      
        }
    }
}